package agricultural.system;

import java.util.*;
import java.time.LocalDateTime;

class Field {
    private String id;
    private String name;
    private double area; // 平方米
    private String crop;
    private EnvironmentData lastEnvironmentData;

    public Field(String id, String name, double area, String crop) {
        this.id = id;
        this.name = name;
        this.area = area;
        this.crop = crop;
    }

    // Getters
    public String getId() { return id; }
    public String getName() { return name; }
    public double getArea() { return area; }
    public String getCrop() { return crop; }
    public EnvironmentData getLastEnvironmentData() { return lastEnvironmentData; }

    // Setter
    public void setLastEnvironmentData(EnvironmentData data) {
        this.lastEnvironmentData = data;
    }
}

class Task {
    public enum Status {
        CREATED, RUNNING, COMPLETED, FAILED
    }

    private String id;
    private String name;
    private Field field;
    private AgriculturalMachine machine;
    private String description;
    private Status status = Status.CREATED;
    private LocalDateTime createTime;
    private LocalDateTime startTime;
    private LocalDateTime endTime;

    public Task(String id, String name, Field field, AgriculturalMachine machine, String description) {
        this.id = id;
        this.name = name;
        this.field = field;
        this.machine = machine;
        this.description = description;
        this.createTime = LocalDateTime.now();
    }

    // Getters
    public String getId() { return id; }
    public String getName() { return name; }
    public Field getField() { return field; }
    public AgriculturalMachine getMachine() { return machine; }
    public String getDescription() { return description; }
    public Status getStatus() { return status; }
    public LocalDateTime getCreateTime() { return createTime; }
    public LocalDateTime getStartTime() { return startTime; }
    public LocalDateTime getEndTime() { return endTime; }

    // Methods
    public void start() {
        this.status = Status.RUNNING;
        this.startTime = LocalDateTime.now();
        System.out.println("任务 [" + name + "] 开始执行");
    }

    public void stop() {
        this.status = Status.COMPLETED;
        this.endTime = LocalDateTime.now();
        System.out.println("任务 [" + name + "] 已完成");
    }
}

class User {
    public enum Role {
        ADMIN, OPERATOR, VIEWER
    }

    private String username;
    private String realName;
    private String password;
    private Role role;

    public User(String username, String realName, String password, Role role) {
        this.username = username;
        this.realName = realName;
        this.password = password;
        this.role = role;
    }

    // Getters
    public String getUsername() { return username; }
    public String getRealName() { return realName; }
    public String getPassword() { return password; }
    public Role getRole() { return role; }
}

class Logger {
    private static Logger instance = new Logger();
    private List<String> logs = new ArrayList<>();

    private Logger() {} // 私有构造函数

    public static Logger getInstance() {
        return instance;
    }

    public void log(String message) {
        String logEntry = LocalDateTime.now() + " - " + message;
        logs.add(logEntry);
        System.out.println(logEntry);
    }

    public void showLog() {
        System.out.println("\n=== 系统日志 ===");
        for (String log : logs) {
            System.out.println(log);
        }
    }
}

class Sprayer implements AgriculturalMachine {
    private String type = "喷药机";
    private MachineState state;
    private PathPlanningStrategy pathStrategy;
    private String location = "原点";
    private double fuelLevel = 95.0;
    private Task currentTask;
    private double pesticideLevel = 100.0;

    public Sprayer() {
        this.state = new IdleState();
        this.pathStrategy = new NormalPathStrategy();
    }

    @Override
    public void start(Task task) {
        this.currentTask = task;
        this.state = new WorkingState();
        state.execute(this);
        System.out.println("开始任务: " + task.getName() + " 在 " + task.getField().getName());
        System.out.println("农药剩余: " + pesticideLevel + "%");
        simulateFuelConsumption();
    }

    @Override
    public void stop() {
        this.state = new MaintenanceState();
        state.execute(this);
        if (currentTask != null) {
            System.out.println("停止任务: " + currentTask.getName());
            currentTask = null;
        }
    }

    @Override
    public void changePath(Field field) {
        System.out.println(pathStrategy.planPath(field, this));
        updateLocation(field);
    }

    @Override
    public void update(EnvironmentData data) {
        System.out.println(type + " 收到 " + data.getField().getName() + " 的环境数据更新");

        if (currentTask != null && !currentTask.getField().getId().equals(data.getField().getId())) {
            System.out.println("当前任务在其他农田，忽略此环境数据");
            return;
        }

        if (data.isHasObstacle()) {
            this.state = new AlertState();
            state.execute(this);
            changePath(data.getField());
        } else if (data.getWindSpeed() > 5) {
            System.out.println(type + " 调整喷洒策略：风速过高，减少喷洒量");
        } else {
            this.state = new WorkingState();
            state.execute(this);
            changePath(data.getField());
        }
    }
